home *** CD-ROM | disk | FTP | other *** search
/ Interplay's Learn to Program Basic (Review Copy) / Learn to Program Basic Review Copy (Interplay)(June 23, 1998).ISO / pc / ltpbasic / projects / namelist.bas < prev    next >
Encoding:
BASIC Source File  |  1998-02-21  |  6.2 KB  |  349 lines

  1.  
  2. Rem Address Book
  3. Rem By Steven Ohmert
  4. Rem 
  5. Rem This will record names, addresses
  6. Rem and phone numbers and allow you to 
  7. Rem save them in a file, NameList.TXT
  8. Rem This file will be loaded each
  9. Rem time you run the program, so
  10. Rem your information is preserved.
  11. Rem In programming, this is called
  12. Rem 'Persistent Data'
  13. Rem 
  14. Rem You can add many more features to
  15. Rem this program on your own, too!
  16.  
  17. Rem Set up array of records
  18. Rem Each 'field' in the record
  19. Rem is identified by a number
  20. Rem which corresponds to the
  21. Rem second array index
  22.  
  23. Let numRecords = 20
  24. Let numFields = 6
  25. Let name = 1
  26. Let addr = 2
  27. Let city = 3
  28. Let state = 4
  29. Let zip = 5
  30. Let phone = 6
  31. Dim Record$(numRecords,numFields)
  32.  
  33. Rem Variables that track
  34. Rem where we are
  35. Let current = 1     ' current active record
  36. Let field = 0       ' current active field
  37. Let totalRecords = 0  ' total number in data base
  38.  
  39. Rem -- Load the file, and start going!
  40. Gosub LoadFile
  41. Gosub ShowRecord
  42. Goto UserInterface
  43.  
  44. Rem Print a record
  45. Rem 
  46. Rem Set current = the record to display
  47. ShowRecord:
  48. CLS
  49. TextColor 166 
  50. Print "Record #";current;" of ";totalRecords
  51. Print
  52. If field = name Then 
  53. TextColor 235 
  54. Else
  55. TextColor 21
  56. Endif
  57. Print "Name:    ";Record$(current,name)
  58.  
  59. If field = addr Then 
  60. TextColor 235 
  61. Else
  62. TextColor 21
  63. Endif
  64. Print "Address: ";Record$(current,addr)
  65.  
  66. If field = city Then 
  67. TextColor 235 
  68. Else
  69. TextColor 21
  70. Endif
  71. Print "City:    ";Record$(current,city)
  72.  
  73. If field = state Then 
  74. TextColor 235 
  75. Else
  76. TextColor 21
  77. Endif
  78. Print "State:   ";
  79. Print Record$(current,state)
  80.  
  81. If field = zip Then 
  82. TextColor 235 
  83. Else
  84. TextColor 21
  85. Endif
  86. Print "Zip:     ";
  87. Print Record$(current,zip)
  88.  
  89. If field = phone Then 
  90. TextColor 235 
  91. Else
  92. TextColor 21
  93. Endif
  94. Print "Phone:   ";Record$(current,phone)
  95.  
  96. Rem Show the menu of options
  97. Position 0,11
  98. TextColor 83 
  99. Print "<PgUp> = previous  <PgDn> = next"
  100. Print "Click on field to edit, press return"
  101. Print
  102. TextColor 67
  103. Print "A";
  104. TextColor 12
  105. Print ")dd ";
  106. TextColor 67
  107. Print "D";
  108. TextColor 12
  109. Print ")elete ";
  110. TextColor 67
  111. Print "S";
  112. TextColor 12
  113. Print ")ave ";
  114. TextColor 67
  115. Print "B";
  116. TextColor 12
  117. Print ")ackup ";
  118. TextColor 67
  119. Print "Q";
  120. TextColor 12
  121. Print ")uit "
  122.  
  123. Return
  124.  
  125. Rem User Interface
  126. Rem This is the 'Main Loop'
  127. Rem the program will run through
  128. Rem these steps until user quits
  129.  
  130. UserInterface:
  131. While TRUE
  132. oldRecord = current
  133. field = 0
  134.  
  135. If Button Then
  136. field = Int(MouseY / 16) - 1
  137. If field < 1 then field = 0
  138. If field > numFields Then field = 0
  139. Endif
  140.  
  141. If KeyDown("PgUp") Then 
  142. current = current -1
  143. While KeyDown("PgUp")
  144. Wend
  145. EndIf
  146.  
  147. If KeyDown("PgDn") Then 
  148. current = current +1
  149. While KeyDown("PgDn")
  150. Wend
  151. If current > totalRecords Then current = totalRecords
  152. EndIf
  153.  
  154. Rem Options
  155. key$ = Inkey$
  156. If key$ = "A" Then
  157. totalRecords = totalRecords + 1
  158. If totalRecords > numRecords Then totalRecords = numRecords
  159. current = totalRecords
  160. field = 1
  161. Rem Setting field to 1 will force
  162. Rem edit code below to activate
  163. Endif
  164. If key$ = "D" Then
  165. Gosub DeleteRecord
  166. Gosub ShowRecord
  167. Endif
  168. If key$ = "S" Then 
  169. Gosub SaveFile
  170. Endif
  171. If key$ = "B" Then
  172. Gosub Backup
  173. Gosub ShowRecord
  174. Endif
  175. If key$ = "Q" Then
  176. Gosub Quit
  177. Endif
  178.  
  179. If current < 1 Then current = 1
  180. If current > numRecords Then current = numRecords
  181.  
  182. Rem If field has been selected
  183. Rem Edit this field and all blank fields
  184. While field <> 0 
  185. Gosub EditRecord
  186. field = field + 1  ' go on to edit the next field (if blank)
  187. If field > numFields Then 
  188. field = 0
  189. Else
  190. If Record$(current,field) <> "" Then
  191. field = 0 ' don't edit a subsequent field if it has something in it already
  192. Endif
  193. Endif
  194. Gosub ShowRecord
  195. Wend
  196.  
  197. If current <> oldRecord Then
  198. Gosub ShowRecord
  199. Endif
  200.  
  201. Wend
  202.  
  203. EditRecord:
  204. If field = 0 then field = 1
  205. Gosub ShowRecord
  206. Position 9,field+1
  207. Print "                               ";
  208. Position 9,field+1
  209. Input new$
  210. If new$ <> "" Then Record$(current,field) = new$
  211. If current > totalRecords Then totalRecords = current
  212. Return
  213.  
  214. Rem Save the list
  215. SaveFile:
  216. FileOpen "NameList.Txt",NEW EXIST
  217. FileWrite totalRecords
  218. For i = 1 to totalRecords
  219. For j = 1 to numFields
  220. A$ = Record$(i,j)
  221. FileWrite A$
  222. Next j
  223. Next i
  224. FileClose
  225. Return
  226.  
  227. Rem Load the list
  228. LoadFile:
  229. i = 1
  230. If FileExist("NameList.Txt") Then
  231. FileOpen "NameList.Txt", EXIST
  232. FileRead totalRecords
  233. For i = 1 to totalRecords
  234. For j = 1 to numFields
  235. FileRead A$
  236. Record$(i,j) = A$
  237. Next j
  238. Next i
  239. FileClose
  240. EndIf
  241. Rem clear the remainder of array
  242. while i < numRecords
  243. For j = 1 to numFields
  244. Record$(i,j) = ""
  245. Next j
  246. i = i+1
  247. Wend
  248.  
  249. Return
  250.  
  251.  
  252. Rem Delete current record
  253. DeleteRecord:
  254. Rem Don't do this if we have nothing to delete
  255. If totalRecords = 0 Then Return
  256.  
  257. Rem Delete by copying everything above the
  258. Rem current record over the top of the one
  259. Rem before it.
  260. For i = current To numRecords-1
  261. For j = 1 to numFields
  262. Record$(i,j) = Record$(i+1,j)
  263. Next j
  264. Next i
  265. Rem We have one fewer records now
  266. totalRecords = totalRecords - 1
  267. Return
  268.  
  269. Rem Backup the file
  270. Backup:
  271. CLS
  272. TextColor 168 'Columbia Blue
  273. Print "Do you want to save your current work"
  274. Print "before backing up? (Y/N) "
  275. q$ = ""
  276. While q$ = ""
  277. q$ = Inkey$
  278. Wend
  279. If q$ = "Y" OR q$ = "y" Then
  280. Gosub SaveFile
  281. Print "Current work saved."
  282. Endif
  283. NameIt:
  284. Print
  285. Print "What do you want to call"
  286. Print "the backup file? ";
  287. Input backup$
  288. If FileExist(backup$) Then 
  289. Print "That file already exists."
  290. Input "Do you want to replace it? ",y$
  291. If Left$(y$,1) = "Y" OR Left$(y$,1) = "y" Then
  292. FileDelete(backup$)
  293. Else
  294. Print "Please choose a different name."
  295. Goto NameIt
  296. EndIf
  297. Endif
  298. Rem Copy the NameList file to the backup name
  299. Rem Open the name list file. Identify this file with a file reference of 1
  300. FileOpen "NameList.txt", EXIST 1
  301. Rem Create the backup file. Identify this file with a file reference of 2
  302. FileOpen backup$, NEW 2
  303. Rem FileRead will return FALSE if
  304. Rem there is no data left
  305. While FileRead A$, 1
  306. FileWrite A$, 2
  307. Wend
  308. FileClose 1
  309. FileClose 2
  310. Print
  311. Print "File backed up to ";backup$
  312. Input "Press Return",a$ 
  313. Print
  314. Return
  315.  
  316. Rem Quit program. Verify with user first, though
  317. Quit:
  318. CLS
  319. TextColor 168 'Columbia Blue
  320. Print "Are you sure you want to Quit? (Y/N) "
  321. q$ = ""
  322. While q$ = ""
  323. q$ = Inkey$
  324. Wend
  325. If q$ <> "y" AND q$ <> "Y" Then
  326. Rem Don't quit. Show record again and return
  327. Gosub ShowRecord
  328. Return
  329. EndIf
  330. Print
  331. Rem Remind user to save file, if they want to
  332. Print "Do you want to save your work"
  333. Print "before quitting? (Y/N) ";
  334. q$ = ""
  335. While q$ = ""
  336. q$ = Inkey$
  337. Wend
  338. If q$ = "y" OR q$ = "Y" Then
  339. Gosub SaveFile
  340. EndIf
  341.  
  342. Rem Now, quit 
  343. CLS
  344. TextColor 21
  345. Print "Goodbye"
  346. End
  347.  
  348.